﻿//客户端applet
package javase.swing;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

class Dealer extends Object implements Serializable {
	private static final long serialVersionUID = 1L;
	String dName;
	String dPwd;
}

public class LoginClient extends JApplet implements ActionListener {
	private static final long serialVersionUID = 1L;
	JPanel panelObject;
	JLabel labelTitle;

	JLabel labelUserName;
	JLabel labelPassword;
	JFrame f1;

	JTextField textUserName;
	JPasswordField textPassword;
	JButton submit;

	GridBagLayout gbObject;
	GridBagConstraints gbc;

	public void init() {
		gbObject = new GridBagLayout();
		gbc = new GridBagConstraints();
		panelObject = (JPanel) getContentPane();
		panelObject.setLayout(gbObject);

		labelTitle = new JLabel("客户登录");
		labelUserName = new JLabel("用户名：");
		labelPassword = new JLabel("密  码：");

		textUserName = new JTextField(15);
		textPassword = new JPasswordField(10);
		submit = new JButton("提  交");

		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.gridx = 1;
		gbc.gridy = 2;
		gbObject.setConstraints(labelTitle, gbc);
		panelObject.add(labelTitle);

		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.gridx = 1;
		gbc.gridy = 5;
		gbObject.setConstraints(labelUserName, gbc);
		panelObject.add(labelUserName);

		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.gridx = 4;
		gbc.gridy = 5;
		gbObject.setConstraints(textUserName, gbc);
		panelObject.add(textUserName);

		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.gridx = 1;
		gbc.gridy = 8;
		gbObject.setConstraints(labelPassword, gbc);
		panelObject.add(labelPassword);

		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.gridx = 4;
		gbc.gridy = 8;
		gbObject.setConstraints(textPassword, gbc);
		panelObject.add(textPassword);

		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.gridx = 4;
		gbc.gridy = 17;
		gbObject.setConstraints(submit, gbc);
		panelObject.add(submit);
		submit.addActionListener(this);

		f1 = new JFrame("操作界面");
		JButton button1, button2, button3;
		button1 = new JButton("Log a Problem");
		button2 = new JButton("Tech Chat");
		button3 = new JButton("Exit");
		f1.getContentPane().setLayout(new FlowLayout());
		f1.getContentPane().add(button1);
		f1.getContentPane().add(button2);
		f1.getContentPane().add(button3);

	}

	public void actionPerformed(ActionEvent ev) {
		Object obj = ev.getSource();
		if (obj == submit) {
			Dealer uobj = new Dealer();
			uobj.dName = textUserName.getText();
			uobj.dPwd = new String(textPassword.getPassword());

			try {
				Socket toServer;// 客户端socket

				// 指出服务器端的ip地址和程序端口
				toServer = new Socket("127.0.0.1", 5000);

				// 对象输出流，以客户端socket的输出流为参数
				ObjectOutputStream streamToServer = new ObjectOutputStream(
						toServer.getOutputStream());
				// 向服务器端输出对象流
				streamToServer.writeObject((Dealer) uobj);

				BufferedReader bf = new BufferedReader(new InputStreamReader(
						toServer.getInputStream()));
				String loginId = bf.readLine();

				if (loginId.length() != 0) {
					getAppletContext().showStatus("您的登录信息:" + loginId);

					f1.setSize(300, 300);
					f1.setVisible(true);

					streamToServer.close();
					bf.close();
				}
			} catch (Exception e) {
				getAppletContext().showStatus("登录时出错");
			}
		}
	}
}
